﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Text;
using my_proxy.Utils;

namespace my_proxy.RequestHandlers
{
    class AnonymousRequestHandler : OpenRequestHandler
    {
        public AnonymousRequestHandler(Configuration cf, HttpListenerContext context, FileReader fr, object fileLock)
            : base(cf, context, fr, fileLock)
        {
            this.Context = context;
            // this.RemoteUrl = this.Context.Request.RawUrl;
            this.FileReader = fr;
            this.FileLock = fileLock;
            this.Conf = cf;
            if (context.Request.RemoteEndPoint != null)
            {
                Log.Info(context.Request.RemoteEndPoint.Address + " " + context.Request.RawUrl);
            }
            else
            {
                string contextErrMsg = "Context error in: " + context.Request.RawUrl;
                Log.Info(contextErrMsg);
                throw new Exception(contextErrMsg);
            }
        }

        protected virtual HttpWebRequest GetRequest()
        {

            // Create a request to the server
            HttpWebRequest requestToServer = (HttpWebRequest)WebRequest.Create(this.RemoteUrl);

            // Set some options
            requestToServer.Method = this.Context.Request.HttpMethod;
            requestToServer.UserAgent = this.Context.Request.UserAgent;
            requestToServer.KeepAlive = requestToServer.KeepAlive;
            requestToServer.Proxy = null;

            // TODO: add UserAgent
            requestToServer.UserAgent = "Mozilla/1.0 (compatible; MSIE 1.00; Windows 3.11";
            // TODO: add Anonymized
            requestToServer.Headers.Add("Anonymized", "true");

            // For POST, write the post data extracted from the incoming request
            if (requestToServer.Method == "POST")
            {

                const int bufferSize = 1024;
                Stream clientStream = this.Context.Request.InputStream;
                byte[] clientPostData = new byte[bufferSize];
                requestToServer.ContentType = this.Context.Request.ContentType;
                int read = clientStream.Read(clientPostData, 0, bufferSize);
                StringBuilder data = new StringBuilder();
                Stream stream = requestToServer.GetRequestStream();
                //read data untile the end of the stream and write it to the rquest sended
                while (read > 0)
                {
                    string tempString = Encoding.UTF8.GetString(clientPostData, 0, clientPostData.Length);
                    data.Append(tempString);
                    stream.Write(clientPostData, 0, read);
                    read = clientStream.Read(clientPostData, 0, bufferSize);
                }
                stream.Close();

                //TODO: check for mails in the request (use data)
            }

            //check for mails in the request headers
            for (int i = 0; i < this.Context.Request.Headers.Count; i++)
            {
                //TODO: check for mails in the Headers[i]
            }
            return requestToServer;

            ///           or null if page not found </returns>
        }
    }
}
